

#import "MTTextToast.h"
#import "MyDeviceToolsObjc.h"


@interface MTTextToast ()

@property (nonatomic, strong) UIView *msgBackView;

@property (nonatomic, strong) UILabel *textLabel;

@property (nonatomic, strong) dispatch_block_t hideBlock;

@end

@implementation MTTextToast

+ (instancetype)showToastWithText:(NSString *)text {
    return [self showToastWithText:text textColor:[UIColor whiteColor] fontSize:16];
}


+ (instancetype)showToastWithText:(NSString *)text
                        textColor:(UIColor *)textColor
                         fontSize:(CGFloat)fontSize {
    
    UIView *superView = [MyDeviceToolsObjc mainWindow];
    
    MTTextToast *toast = [[MTTextToast alloc] init];
    toast.frame = CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height);
    toast.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    
    
    UIView *backView = [UIView new];
    backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
    backView.layer.cornerRadius = 8.0;
    backView.clipsToBounds = YES;
    [toast addSubview:backView];
    toast.msgBackView = backView;
    
    
    UILabel *textLabel = [[UILabel alloc] init];
    textLabel.text = text;
    textLabel.textColor = textColor;
    textLabel.font = [UIFont systemFontOfSize:fontSize];
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.numberOfLines = 0;
    [backView addSubview:textLabel];
    toast.textLabel = textLabel;
    
    [superView addSubview:toast];
    
    CGSize maxSize = CGSizeMake(superView.frame.size.width - 80, MAXFLOAT);
    CGSize textSize = [text boundingRectWithSize:maxSize
                                        options:NSStringDrawingUsesLineFragmentOrigin
                                     attributes:@{NSFontAttributeName: textLabel.font}
                                        context:nil].size;
    
    textLabel.frame = CGRectMake(16, 12, textSize.width, textSize.height);
    backView.frame = CGRectMake((superView.frame.size.width - textSize.width - 32) / 2,
                             (superView.frame.size.height - textSize.height - 24) / 2,
                             textSize.width + 32,
                             textSize.height + 24);
    
//    toast.alpha = 0.0;
//    
//    [UIView animateWithDuration:0.2 animations:^{
//        toast.alpha = 1.0;
//    } completion:^(BOOL finished) {
        __weak typeof(toast) weakToast = toast;
        dispatch_block_t hideBlock = ^{
            __strong typeof(weakToast) strongToast = weakToast;
            if (strongToast) {
                [strongToast hide];
            }
        };
        
        toast.hideBlock = hideBlock;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), hideBlock);
//    }];
    
    return toast;
}

- (void)hide {
//    [UIView animateWithDuration:0.2 animations:^{
//        self.alpha = 0.0;
//    } completion:^(BOOL finished) {
        self.hideBlock = nil;
        [self removeFromSuperview];
//    }];
}





@end
